package fr.esiag.isidis.fi.pdsTeam.swing;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


import fr.esiag.isidis.fi.pdsTeam.beans.Event;
import fr.esiag.isidis.fi.pdsTeam.interfaces.IInjector;

public class PrincipalFrame extends JFrame implements ActionListener{
	
	private HashMap<Integer, String> sourcesTrain;
	private HashMap<Integer, String> sourcesStation;
	private HashMap<Integer, String> sourcesTunnel;
	private HashMap<Integer,String> messagesTrain;
	private HashMap<Integer,String> messagesStation;
	private HashMap<Integer,String> messagesTunnel;
	IInjector injector;
	JComboBox sources_combo;
	JComboBox messages_combo;
	
	public PrincipalFrame(IInjector injector) {
		
		this.injector = injector;
		fillSources();
		fillMessages();
		
		setTitle("Injector");
		JPanel principalPanel = new JPanel();
		principalPanel.setLayout(new GridLayout(3, 0));


		JPanel firstEvent_panel = new JPanel();
		JLabel firstEvent_label = new JLabel("Sources");
		JComboBox firstEvent_combo = new JComboBox(getSourcesType());
		firstEvent_panel.setLayout(new FlowLayout());
		firstEvent_panel.add(firstEvent_label);
		firstEvent_panel.add(firstEvent_combo);
		principalPanel.add(firstEvent_panel);
		firstEvent_combo.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				JComboBox combo = (JComboBox) e.getSource();
				String sel = (String)combo.getSelectedItem();
				System.out.println(sel);
				if(sel.contains("Train")){
					sources_combo.removeAllItems();
					for (int i = 0; i < getTrainSources().size(); i++) {
						sources_combo.addItem(getTrainSources().get(i));
					}
					messages_combo.removeAllItems();
					for (int i = 0; i < getTrainMessages().size(); i++) {
						messages_combo.addItem(getTrainMessages().get(i));
					}
					
				}else if(sel.contains("Tunnel")){
					sources_combo.removeAllItems();
					for (int i = 0; i < getTunnelSources().size(); i++) {
						sources_combo.addItem(getTunnelSources().get(i));
					}
					messages_combo.removeAllItems();
					for (int i = 0; i < getTunnelMessages().size(); i++) {
						messages_combo.addItem(getTunnelMessages().get(i));
					}
				}else{
					sources_combo.removeAllItems();
					for (int i = 0; i < getStationSources().size(); i++) {
						sources_combo.addItem(getStationSources().get(i));
					}
					messages_combo.removeAllItems();
					for (int i = 0; i < getStationMessages().size(); i++) {
						messages_combo.addItem(getStationMessages().get(i));
					}
				}
			}
		});
		
		JPanel secondEvent_panel = new JPanel();
		JLabel secondEvent_label = new JLabel("Sources");
		sources_combo = new JComboBox(getTrainSources());
		secondEvent_panel.setLayout(new FlowLayout());
		secondEvent_panel.add(secondEvent_label);
		secondEvent_panel.add(sources_combo);
		principalPanel.add(secondEvent_panel);
		
		JPanel thirdEvent_panel = new JPanel();
		JLabel thirdEvent_label = new JLabel("Message");
		messages_combo = new JComboBox(getTrainMessages());
		JButton thirdEvent_button = new JButton("Inject Event");
		thirdEvent_panel.setLayout(new FlowLayout());
		thirdEvent_panel.add(thirdEvent_label);
		thirdEvent_panel.add(messages_combo);
		thirdEvent_panel.add(thirdEvent_button);	
		principalPanel.add(thirdEvent_panel);
		thirdEvent_button.addActionListener(this);
	    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(principalPanel);
		pack();
		setVisible(true);
	}
	

	
	public void fillSources(){
		sourcesTrain = new HashMap();
		sourcesTrain.put(10, "capteur de temperature Train");
		sourcesTrain.put(11, "capteur de pression");
		sourcesTrain.put(12, "capteur de proximite");
		sourcesTrain.put(13, "capteur de train 2");
		sourcesTrain.put(14,"capteur de train 3" );
		sourcesTrain.put(15,"capteur de train 4" );
		sourcesTrain.put(16,"capteur de train 5" );
		sourcesTrain.put(17,"capteur de train 6");
		sourcesTrain.put(18, "capteur de train 7");
		sourcesStation = new HashMap();
		sourcesStation.put(20,"capteur de temperature Station");
		sourcesStation.put(21,"capteur de luminosite");
		sourcesTunnel = new HashMap();
		sourcesTunnel.put(22,"capteur de pollution CO");
	
	}
	
	
	public void fillMessages(){
		messagesTrain = new HashMap();
		messagesTrain.put(100, "Message 10-1");
		messagesTrain.put(101, "Message 10-2");
		messagesTrain.put(102, "Message 10-3");
		messagesTrain.put(103, "Message 10-4");
		messagesTrain.put(104, "Message 10-5");
		messagesTrain.put(105, "Message 10-6");
		messagesStation = new HashMap();
		messagesStation.put(200,"Message 20-1");
		messagesStation.put(201,"Message 20-2");
		messagesTunnel = new HashMap();
		messagesTunnel.put(220,"Message 22-1");
		messagesTunnel.put(220,"Message 22-2");
	
	}
	

	public Vector<String> getSourcesType(){
		Vector<String> s = new Vector();
		s.add("Train");
		s.add("Station");
		s.add("Tunnel");
		return s;
	}
	
	public Vector<String> getTrainSources(){
		Vector<String> s = new Vector();
		for ( Iterator iter = sourcesTrain.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			s.add((String) ent.getValue());
		}
		return s;
	}
	
	public Vector<String> getTunnelSources(){
		Vector<String> s = new Vector();
		for ( Iterator iter = sourcesTunnel.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			s.add((String) ent.getValue());
		}
		return s;
	}
	
	public Vector<String> getStationSources(){
		Vector<String> s = new Vector();
		for ( Iterator iter = sourcesStation.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			s.add((String) ent.getValue());
		}
		return s;
	}
	
	public Vector<String> getStationMessages(){
		Vector<String> s = new Vector();
		for ( Iterator iter = messagesStation.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			s.add((String) ent.getValue());
		}
		return s;
	}
	
	public Vector<String> getTunnelMessages(){
		Vector<String> s = new Vector();
		for ( Iterator iter = messagesTunnel.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			s.add((String) ent.getValue());
		}
		return s;
	}
	
	public Vector<String> getTrainMessages(){
		Vector<String> s = new Vector();
		for ( Iterator iter = messagesTrain.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			s.add((String) ent.getValue());
		}
		return s;
	}



	public void actionPerformed(ActionEvent e) {
		Event event = new Event();
		String macAdd = (String) sources_combo.getSelectedItem();
		String mess =  (String)messages_combo.getSelectedItem();
		Integer codeEvent = 0;
		
		for ( Iterator iter = messagesTrain.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			if(((String) ent.getValue()).contains(mess))
					codeEvent = (Integer) ent.getKey();
		}
		for ( Iterator iter = messagesTunnel.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			if(((String) ent.getValue()).contains(mess))
				codeEvent = (Integer) ent.getKey();
		}
		for ( Iterator iter = messagesStation.entrySet().iterator(); iter.hasNext(); ) {
			Map.Entry ent = (Map.Entry) iter.next();
			if(((String) ent.getValue()).contains(mess))
				codeEvent = (Integer) ent.getKey();
		}
		
		injector.instancierEvent(codeEvent,macAdd);
		
		System.out.println(codeEvent+ " " + macAdd);
	}


}
